package org.awa.demo.opencv.bean;

import lombok.AllArgsConstructor;

/**
 * @author xxf
 */
public class RgbRange {

    private double startLeqt;
    private double endLeqt;

    private Rgb startRgb;
    private Rgb endRgb;

    private double leqtRange;
    private double[] rgbRange = new double[3];

    public RgbRange(double startLeqt,double endLeqt,Rgb startRgb,Rgb endRgb){
        this.startLeqt = startLeqt;
        this.endLeqt = endLeqt;
        this.startRgb = startRgb;
        this.endRgb = endRgb;
        this.leqtRange = endLeqt - startLeqt;
        double[] valueStartArr = startRgb.getVal();
        double[] valueEndArr = endRgb.getVal();
        this.rgbRange[0] = valueEndArr[0] - valueStartArr[0];
        this.rgbRange[1] = valueEndArr[1] - valueStartArr[1];
        this.rgbRange[2] = valueEndArr[2] - valueStartArr[2];
    }

    public boolean checkLeqt(double leqt){
        return leqt <= endLeqt;
    }


    public Rgb getRgb(double leqt){
        if(leqt < startLeqt || leqt > endLeqt){
            return Rgb.ZERO;
        }

        if(leqt == startLeqt){
            return  startRgb;
        }else if(leqt == endLeqt){
            return endRgb;
        }

        return new Rgb(getValRange(leqt,0),getValRange(leqt,1),getValRange(leqt,2));
    }

    private double getValRange(double leqt,int index){
        return ((leqt - startLeqt) * rgbRange[index] / leqtRange) + startRgb.getVal()[index];
    }
}
